﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Services;
using System.Web.Services;
using MS.InformationManagement.BLL.DocumentScheduler;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.Support;

namespace MS.InformationManagement.Services
{
    /// <summary>
    /// Summary description for DocumentSchedulerService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [ScriptService]
    public class DocumentSchedulerService : System.Web.Services.WebService
    {
        [WebMethod]
        public void ScheduleDocument(string libraryGuid, int documentID, DateTime scheduleDate)
        {
            try
            {
                DocumentSchedulerBLL.ScheduleDocument(new Guid(libraryGuid), documentID, scheduleDate);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }

        [WebMethod]
        public List<Radicado> FetchDocuments(DateTime start, DateTime end, string libraryGuid) {
            try
            {
                return DocumentSchedulerBLL.GetScheduledDocuments(new Guid(libraryGuid), start, end).ToList();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }

        [WebMethod]
        public void UnscheduleDocument(string library, int documentID) {
            try
            {
                DocumentSchedulerBLL.UnscheduleDocument(library, documentID);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }

        [WebMethod]
        public List<Radicado> FetchUnansweredDocuments(DateTime start, DateTime end, string libraryGuid)
        {
            try
            {
                return DocumentSchedulerBLL.GetUnansweredDocuments(new Guid(libraryGuid), start, end).ToList();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw ex;
            }
        }

        [WebMethod]
        public bool IsHoliday(DateTime date) {
            try
            {
                return BusinessDateHelper.IsBusinessValidDay(date);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }

        [WebMethod]
        public List<FestivoDTO> GetHolidays(DateTime startDate, DateTime endDate)
        {
            try
            {
                return BusinessDateHelper.GetHolidays(startDate, endDate).ToList();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }

        [WebMethod]
        public Radicado GetDocument(string library, int documentID) {
            try
            {
                return DocumentSchedulerBLL.GetRadicado(library, documentID);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                    SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
        }
        
    }
}
